/* Copyright (C) 2008, 2009, 2010 Faindu GmbH, Switzerland.
* All Rights Reserved. The following is Source Code and is subject to all restrictions 
* on such code as contained in the license accompanying this project. */
package blocstac.editimage.base
{

import blocstac.api.blocbox.ImageLoader;
import blocstac.api.blocbox.MimeType;

import flash.display.BitmapData;
import flash.utils.ByteArray;

import flexunit.framework.TestCase;

public class EditImageDataImplTest extends TestCase
{
	
    //==========================================================================
    //
    //  Test set up and tear down
    //
    //--------------------------------------------------------------------------  
	private var editImageData:EditImageDataImpl
	private var imageModel:ImageModel
	private var imageLoader:ImageLoader
	
	override public function setUp(): void
	{
		editImageData = new EditImageDataImpl()
		imageModel = new ImageModel()
		imageLoader = new ImageLoaderImpl(null)
	}
	
	override public function tearDown():void
	{
	}
	
    //==========================================================================
    //
    //  Tests
    //
    //--------------------------------------------------------------------------
	[Test]
	public function test_config(): void
	{
		editImageData.config = <editimage>
									<angle>11</angle>
									<scaleX>23</scaleX>
									<scaleY>57</scaleY>
								</editimage>
								
		assertEquals("<editimage><angle>11</angle><scaleX>23</scaleX><scaleY>57</scaleY></editimage>", editImageData.config)
		assertNull(editImageData.imageLoader)
		assertNull(editImageData.mimeType)
	}

	[Test]
	public function test_config_null(): void
	{
		editImageData.config = null
		assertEquals("<editimage><angle>0</angle><scaleX>1</scaleX><scaleY>1</scaleY></editimage>", editImageData.config)
		assertNull(editImageData.imageLoader)
		assertNull(editImageData.mimeType)
	}

	[Test]
	public function test_config_with_invalid_XML(): void
	{
		editImageData.config = "blabla"
		assertNull(editImageData.config)
		assertNull(editImageData.imageLoader)
		assertNull(editImageData.mimeType)
	}
	
	[Test]
	public function test_copyTo(): void
	{
		editImageData.config = <editimage>
									<angle>11</angle>
									<scaleX>23</scaleX>
									<scaleY>57</scaleY>
								</editimage>
								
		editImageData.copyTo(imageModel)
		assertEquals(11, imageModel.angle)
		assertEquals(23, imageModel.editedScaleX)
		assertEquals(57, imageModel.editedScaleY)
		
		assertNull(editImageData.imageLoader)
		assertNull(editImageData.mimeType)
	}
	
	[Test]
	public function test_mimeType():void {
		assertNull(editImageData.mimeType)
		editImageData.mimeType = new MimeType(MimeType.MIMETYPE_PNG)
		assertEquals(MimeType.MIMETYPE_PNG, editImageData.mimeType.toString())
	}
	
	[Test]
	public function test_createFrom_with_NO_bytes():void {
		imageModel.angle = 11
		imageModel.editedScaleX = 23
		imageModel.editedScaleY = 57
		imageModel.mimeType = new MimeType(MimeType.MIMETYPE_PNG)
		
		editImageData.createFrom(imageModel, imageLoader)
		assertNull(editImageData.imageLoader.bytes)
		assertEquals(MimeType.MIMETYPE_PNG, editImageData.mimeType.toString())
		assertEquals("<editimage><angle>11</angle><scaleX>23</scaleX><scaleY>57</scaleY></editimage>", editImageData.config)
	}

	[Test]
	public function test_createFrom_while_imageModel_is_encoding_PNG():void {
		imageModel.angle = 11
		imageModel.editedScaleX = 23
		imageModel.editedScaleY = 57
		imageModel.mimeType = new MimeType(MimeType.MIMETYPE_PNG)
		var imageData:BitmapData = new BitmapData(5, 7)
		imageModel.updateImageData(imageData)
		imageModel.updateImageData(imageData.clone())
		imageModel._bytes = null
		
		editImageData.createFrom(imageModel, imageLoader)
		var bytes:ByteArray = editImageData.imageLoader.bytes
		assertNotNull(bytes)
		assertEquals(72, bytes.length)
		assertEquals(MimeType.MIMETYPE_PNG, editImageData.mimeType.toString())
		assertEquals("<editimage><angle>11</angle><scaleX>23</scaleX><scaleY>57</scaleY></editimage>", editImageData.config)
	}

	[Test]
	public function test_createFrom_while_imageModel_is_encoding_PNG_from_GIF():void {
		imageModel.angle = 11
		imageModel.editedScaleX = 23
		imageModel.editedScaleY = 57
		imageModel.mimeType = new MimeType(MimeType.MIMETYPE_GIF)
		var imageData:BitmapData = new BitmapData(5, 7)
		imageModel.updateImageData(imageData)
		imageModel.updateImageData(imageData.clone())
		imageModel._bytes = null
		
		editImageData.createFrom(imageModel, imageLoader)
		var bytes:ByteArray = editImageData.imageLoader.bytes
		assertNotNull(bytes)
		assertEquals(72, bytes.length)
		assertEquals(MimeType.MIMETYPE_PNG, editImageData.mimeType.toString())
		assertEquals("<editimage><angle>11</angle><scaleX>23</scaleX><scaleY>57</scaleY></editimage>", editImageData.config)
	}

	[Test]
	public function test_createFrom_while_imageModel_is_encoding_JPG_from_any_Mimetype():void {
		imageModel.angle = 11
		imageModel.editedScaleX = 23
		imageModel.editedScaleY = 57
		imageModel.mimeType = new MimeType("anyImageMimetype")
		var imageData:BitmapData = new BitmapData(5, 7)
		imageModel.updateImageData(imageData)
		imageModel.updateImageData(imageData.clone())
		imageModel._bytes = null
		
		editImageData.createFrom(imageModel, imageLoader)
		var bytes:ByteArray = editImageData.imageLoader.bytes
		assertNotNull(bytes)
		assertEquals(648, bytes.length)		
		assertEquals(MimeType.MIMETYPE_JPG, editImageData.mimeType.toString())
		assertEquals("<editimage><angle>11</angle><scaleX>23</scaleX><scaleY>57</scaleY></editimage>", editImageData.config)
	}

}
}